﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using NewAndong.Business;
using NewAndong.Model;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
using Leeder.Logger;
using GTMC.Enum;

namespace NewAndong.Forms
{
    public partial class FrmExceptionRecord : UIPage
    {
        public LogOperate LogObj => Log.GetLogger(this.GetType());

        private SystemDeviceMonitorBLL bllSystemDeviceMonitor = new SystemDeviceMonitorBLL();
        private PlanMonitorBLL bllLogisticsSystemPlanMonitor = new PlanMonitorBLL();
        
        /// <summary>
        /// 判断窗体是否首次加载
        /// </summary>
        bool isLoad = true;
        /// <summary>
        /// 数据表每页行数
        /// </summary>
        int pageSize = 35;       
        public FrmExceptionRecord()
        {
            InitializeComponent();
        }

        private void FrmExceptionRecord_Load(object sender, EventArgs e)
        {
            //设置开始时间为两天前
            this.dpJDStartDate.Value = DateTime.Now.AddDays(-2);
            this.dpJDEndDate.Value = DateTime.Now;

            //设置开始时间为一星期前
            this.dpStartDate.Value = DateTime.Now.AddDays(-7);
            this.dpEndDate.Value = DateTime.Now;

            if (!isLoad)
                return;
            isLoad = false;

            uiPagination1.Style = UIStyle.Blue;
            uiPagination1.ShowJumpButton = true;
            uiPagination2.Style = UIStyle.Blue;
            uiPagination2.ShowJumpButton = true;

            dgvHistory.AutoGenerateColumns = false;
            dgvHistory.AllowUserToAddRows = false;
            dgvHistory.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHistory.ReadOnly = true;
            dgvHistory.DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
            dgvHistory.AddColumn("时间", "ExceptionDate", 25);
            dgvHistory.AddColumn("设备ID", "DeviceID", 25);
            dgvHistory.AddColumn("设备类型", "DeviceType", 25);
            dgvHistory.AddColumn("设备名称", "DeviceName", 25);
            dgvHistory.AddColumn("异常描述", "ExceptionDescription", 25);

            //设置分页控件总数
            //uiPagination1.TotalCount = SystemDeviceMonitorList.Count;
            //设置分页控件每页数量
            uiPagination1.PageSize = pageSize;

            dgvHistoryJD.AutoGenerateColumns = false;
            dgvHistoryJD.AllowUserToAddRows = false;
            dgvHistoryJD.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHistoryJD.ReadOnly = true;
            dgvHistoryJD.DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
            dgvHistoryJD.AddColumn("记录时间", "CreateTime", 12);
            dgvHistoryJD.AddColumn("部品名称", "ProductName", 14);
            dgvHistoryJD.AddColumn("开捆类型", "AreaName", 12);
            //dgvHistoryJD.AddColumn("开始时间", "StartTime", 12);
            //dgvHistoryJD.AddColumn("结束时间", "EndTime", 12);
            dgvHistoryJD.AddColumn("延时发生时间", "WorkStartTime", 12);
            dgvHistoryJD.AddColumn("延时结束时间", "WorkEndTime", 12);
            dgvHistoryJD.AddColumn("异常描述", "Description", 14);

            //设置分页控件总数
            //uiPagination2.TotalCount = LogisticsSystemPlanMonitorList.Count;
            //设置分页控件每页数量
            uiPagination2.PageSize = pageSize;

            Search_Exception();
            Search_Direction();
        }

        private void FrmExceptionRecord_SizeChanged(object sender, EventArgs e)
        {

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            Search_Exception();
        }

        /// <summary>
        /// 查询
        /// </summary>
        private void Search_Exception()
        {
            DateTime? startDate = dpStartDate.Value;
            DateTime? endDate = dpEndDate.Value;
            string deviceType = "";
            if (!cbdeviceType.Text.Equals("全部"))
            {
                deviceType = cbdeviceType.Text;
            }
            ////设置分页控件总数
            uiPagination1.TotalCount = bllSystemDeviceMonitor.GetModelCountByWhere(startDate, endDate, deviceType);
            uiPagination1_PageChanged(null, null, 1, pageSize);
        }

        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            try
            {
                DateTime? startDate = dpStartDate.Value;
                DateTime? endDate = dpEndDate.Value;
                string deviceType = "";
                if (!cbdeviceType.Text.Equals("全部"))
                {
                    deviceType = cbdeviceType.Text;
                }
                var monitorList = bllSystemDeviceMonitor.GetHistoryListByWhere(startDate, endDate, deviceType, pageIndex, count);
                monitorList.ForEach(x => { x.DeviceType = string.IsNullOrEmpty(x.DeviceId)?x.DeviceName:x.DeviceId; if (!string.IsNullOrEmpty(x.DeviceId) && x.DeviceId.ToLower().StartsWith("led")) x.DeviceType = "LED"; });
                dgvHistory.DataSource = monitorList;
                dgvHistory.Refresh();
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.Message);
            }
        }

        private void Search_Direction()
        {
            DateTime? startDate = dpJDStartDate.Value;
            DateTime? endDate = dpJDEndDate.Value;
            string jdType = "";
            if (!cbJdType.Text.Equals("全部"))
            {
                jdType = cbJdType.Text;
            }
            ////设置分页控件总数
            uiPagination2.TotalCount = bllLogisticsSystemPlanMonitor.GetModelCountByWhere(startDate, endDate, jdType);
            uiPagination2_PageChanged(null, null, 1, pageSize);
        }

        private void uiPagination2_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            try
            {
                DateTime? startDate = dpJDStartDate.Value;
                DateTime? endDate = dpJDEndDate.Value;
                string jdType = "";
                if (!cbJdType.Text.Equals("全部"))
                {
                    jdType = cbJdType.Text;
                }
                dgvHistoryJD.DataSource = bllLogisticsSystemPlanMonitor.GetHistoryListByWhere(startDate, endDate, jdType, pageIndex, count);
                dgvHistoryJD.Refresh();
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.Message);
            }
        }

        private void btnQueryJD_Click(object sender, EventArgs e)
        {
            Search_Direction();
        }

    }
}
